﻿using System;
using Microsoft.SqlServer.Dts.Runtime.Wrapper;


namespace Table_Storage_Component
{
    class Column
    {
        public Column(string columnName, string typeName, string valueAsString)
        {
            this.ColumnName = columnName;
            this.Length = 0;
            this.ClrType = this.GetType(typeName);
            this.DtsType = this.GetSsisType(typeName);
            this.Value = this.GetValue(this.DtsType, valueAsString);
            
        }

        public string ColumnName { get; private set; }
        public Type ClrType { get; private set; }
        public DataType DtsType { get; private set; }
        public object Value { get; private set; }
        public int Length { get; private set; }

        private Type GetType(string type)
        {
            switch (type)
            {
                case "Edm.String": return typeof(string);
                case "Edm.Int32": return typeof(int);
                case "Edm.Int64": return typeof(long);
                case "Edm.Double": return typeof(double);
                case "Edm.Boolean": return typeof(bool);
                case "Edm.DateTime": return typeof(DateTime);
                case "Edm.Binary": return typeof(byte[]);
                case "Edm.Guid": return typeof(Guid);
                default: return typeof(string); //throw new NotSupportedException(string.Format("Unsupported data type [{0}]", type));
            }
            
        }
        // a remplacer par http://msdn.microsoft.com/en-us/library/microsoft.sqlserver.dts.pipeline.pipelinecomponent.datarecordtypetobuffertype.aspx
        private DataType GetSsisType(string type)
        {
            switch (type)
            {
                case "Edm.String": return DataType.DT_NTEXT;
                case "Edm.Binary": return DataType.DT_IMAGE;
                case "Edm.Int32": return DataType.DT_I4;
                case "Edm.Int64": return DataType.DT_I8;
                case "Edm.Boolean": return DataType.DT_BOOL;
                case "Edm.DateTime": return DataType.DT_DATE;
                case "Edm.Guid": return DataType.DT_GUID;
                case "Edm.Double": return DataType.DT_R8;
                default: this.Length = 4000;
                    return DataType.DT_WSTR; //throw new NotSupportedException(string.Format("Unsupported data type {0}", type));
            }
        }

        private object GetValue(DataType dtsType, string valueAsString)
        {
            switch (dtsType)
            {
                case DataType.DT_NTEXT: return valueAsString;
                case DataType.DT_IMAGE: return Convert.FromBase64String(valueAsString);
                case DataType.DT_BOOL: return bool.Parse(valueAsString);
                case DataType.DT_DATE: return DateTime.Parse(valueAsString);
                case DataType.DT_GUID: return new Guid(valueAsString);
                case DataType.DT_I2: return Int32.Parse(valueAsString);
                case DataType.DT_I4: return Int64.Parse(valueAsString);
                case DataType.DT_R8: return double.Parse(valueAsString);
                case DataType.DT_WSTR: return valueAsString;
                default: throw new NotSupportedException(string.Format("Unsupported data type {0}", dtsType));
            }
        }
    }
}
